<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="企业类型" prop="companyType">
        <el-select
          v-model="queryParams.companyType"
          placeholder="请选择企业类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in companyTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二维码等级" prop="zxingColor">
        <el-select
          v-model="queryParams.zxingColor"
          placeholder="请选择二维码等级"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in zxingColorOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="显示状态" prop="showStatus">
        <el-select
          v-model="queryParams.showStatus"
          placeholder="请选择显示状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in showStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['app:appCompany:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['app:appCompany:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['app:appCompany:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['app:appCompany:export']"
          >导出</el-button
        >
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button
            size="mini"
            circle
            icon="el-icon-refresh"
            @click="handleQuery"
          />
        </el-tooltip>
        <el-tooltip
          class="item"
          effect="dark"
          :content="showSearch ? '隐藏搜索' : '显示搜索'"
          placement="top"
        >
          <el-button
            size="mini"
            circle
            icon="el-icon-search"
            @click="showSearch = !showSearch"
          />
        </el-tooltip>
      </div>
    </el-row>

    <el-table
      v-loading="loading"
      :data="appCompanyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column
        label="企业类型"
        align="center"
        prop="companyType"
        :formatter="companyTypeFormat"
      />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column
        label="二维码等级"
        align="center"
        prop="zxingColor"
        :formatter="zxingColorFormat"
      />
      <el-table-column label="二维码" align="center" prop="zxingUrl">
        <template slot-scope="scope">
          <div class="demo-image__preview">
            <el-image
              style="height: 30%; width: 30%"
              :src="scope.row.zxingUrl"
              :zIndex="9999"
              :preview-src-list="[scope.row.zxingUrl]"
              @click.stop="handleClickItem"
            ></el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" />
      <!--      <el-table-column label="经度" align="center" prop="coordinateX" />
      <el-table-column label="纬度" align="center" prop="coordinateY" />-->
      <el-table-column
        label="显示状态"
        align="center"
        prop="showStatus"
        :formatter="showStatusFormat"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:appCompany:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:appCompany:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业类型">
          <el-select v-model="form.companyType" placeholder="请选择企业类型">
            <el-option
              v-for="dict in companyTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="二维码等级">
          <el-select v-model="form.zxingColor" placeholder="请选择二维码等级">
            <el-option
              v-for="dict in zxingColorOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="二维码路径" prop="zxingUrl">
          <el-input
            v-model="form.zxingUrl"
            placeholder="请输入二维码路径"
            readonly
          />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <!--        <el-form-item label="经度" prop="coordinateX">
          <el-input v-model="form.coordinateX" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="coordinateY">
          <el-input v-model="form.coordinateY" placeholder="请输入纬度" />
        </el-form-item>-->
        <el-form-item label="显示状态">
          <el-select v-model="form.showStatus" placeholder="请选择显示状态">
            <el-option
              v-for="dict in showStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="简介" prop="simpleDesc">
          <el-input
            v-model="form.simpleDesc"
            type="textarea"
            placeholder="简述"
            :autosize="{ minRows: 2, maxRows: 4 }"
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="运营情况" prop="operationalStatus">
          <el-input
            v-model="form.operationalStatus"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="年金额" prop="yearMoney">
          <el-input-number
            v-model="form.yearMoney"
            placeholder="请输入年金额"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAppCompany,
  getAppCompany,
  delAppCompany,
  addAppCompany,
  updateAppCompany,
  Company,
} from "@/api/app/appCompany";

export default {
  name: "AppCompany",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业表格数据
      appCompanyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 企业类型字典
      companyTypeOptions: [],
      // 二维码等级字典
      zxingColorOptions: [],
      // 显示状态字典
      showStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyType: null,
        name: null,
        zxingColor: null,
        description: null,
        address: null,
        coordinateX: null,
        coordinateY: null,
        showStatus: null,
        simpleDesc: null,
        operationalStatus: null,
        yearMoney: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.Company("0a450baf7ccc4eb09296a771e10479de").then((res) => {
      console.log(res, "666");
    });
    this.getList();
    this.getDicts("company_type").then((response) => {
      this.companyTypeOptions = response.data;
    });
    this.getDicts("zxing_color").then((response) => {
      this.zxingColorOptions = response.data;
    });
    this.getDicts("sys_yes_no").then((response) => {
      this.showStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询企业列表 */
    getList() {
      this.loading = true;
      listAppCompany(this.queryParams).then((response) => {
        this.appCompanyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 企业类型字典翻译
    companyTypeFormat(row, column) {
      return this.selectDictLabel(this.companyTypeOptions, row.companyType);
    },
    // 二维码等级字典翻译
    zxingColorFormat(row, column) {
      return this.selectDictLabel(this.zxingColorOptions, row.zxingColor);
    },
    // 显示状态字典翻译
    showStatusFormat(row, column) {
      return this.selectDictLabel(this.showStatusOptions, row.showStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyType: null,
        name: null,
        zxingColor: null,
        description: null,
        zxingUrl: null,
        address: null,
        coordinateX: null,
        coordinateY: null,
        showStatus: null,
        simpleDesc: null,
        operationalStatus: null,
        yearMoney: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAppCompany(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAppCompany(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addAppCompany(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除企业编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delAppCompany(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "app/appCompany/export",
        {
          ...this.queryParams,
        },
        `app_appCompany.xlsx`
      );
    },
    handleClickItem(row) {
      this.$nextTick(() => {
        // 获取遮罩层dom
        let domImageMask = document.querySelector(".el-image-viewer__mask");
        if (!domImageMask) {
          return;
        }
        domImageMask.addEventListener("click", () => {
          // 点击遮罩层时调用关闭按钮的 click 事件
          document.querySelector(".el-image-viewer__close").click();
        });
      });
    },
  },
};
</script>
